<template>
  <div style="min-width:400px;max-width: 500px;margin: auto auto 20px;display: flex;align-items: center">
    <el-input style="min-width: 200px;max-width: 400px" v-model="doi" placeholder="请输入要修改的文档DOI"/>
    <el-button style="margin-left: 10px" @click="search">查询</el-button>
  </div>
  <el-form v-if="doc&&doc.id" label-position="top" style="min-width: 400px;max-width: 600px;margin: auto;">
    <el-form-item label="文献编号">
      <el-input v-model="doc.doi" placeholder="请输入文章编号" @input="checkDoi"/>
    </el-form-item>
    <el-form-item label="文献标题">
      <el-input v-model="doc.content.title" placeholder="请输入标题"/>
    </el-form-item>
    <el-form-item label="文献关键词">
      <el-input v-model="kws" placeholder="请输入关键词，多个关键词用 ; 分割" @input="checkKeywordsInput"/>
    </el-form-item>
    <el-form-item label="第一作者">
      <div v-if="doc?.firstAuthor" class="author-desc">{{ authorDesc(doc.firstAuthor) }}</div>
      <div class="unselect-author" v-else>未选择第一作者</div>
      <add-one-author-component v-model="doc.firstAuthor" style="display: inline-block;margin-left: 10px"/> <!--增加作者按钮-->
      <el-button style="margin-left: 10px" v-if="doc.firstAuthor" @click="showAuthor(doc.firstAuthor)">详情</el-button>
      <el-button style="margin-left: 10px" v-if="doc.firstAuthor" type="danger" @click="delAuthor(true)">删除</el-button>
    </el-form-item>
    <el-form-item label="通讯作者">
      <div v-if="doc?.correspondingAuthor" class="author-desc">{{ authorDesc(doc.correspondingAuthor) }}</div>
      <div class="unselect-author" v-else>未选择通讯作者</div>
      <add-one-author-component v-model="doc.correspondingAuthor" style="display: inline-block;margin-left: 10px"/> <!--增加作者按钮-->
      <el-button style="margin-left: 10px" v-if="doc.correspondingAuthor" @click="showAuthor(doc.correspondingAuthor)">详情</el-button>
      <el-button style="margin-left: 10px" v-if="doc.correspondingAuthor" type="danger" @click="delAuthor(false)">删除</el-button>
    </el-form-item>
    <el-form-item label="文献摘要">
      <el-input v-model="doc.content.ab" type="textarea" placeholder="请输入摘要" rows="6"/>
    </el-form-item>
    <el-form-item label="文献类别">
      <el-input v-model="doc.category" placeholder="请输入文献类别"/>
    </el-form-item>
    <el-form-item label="学科分类">
      <el-input v-model="doc.disciplineClassification" placeholder="请输入学科分类"/>
    </el-form-item>
    <el-form-item label="文献语言">
      <el-input v-model="doc.language" placeholder="请输入语言"/>
    </el-form-item>
    <el-form-item label="出版物标题">
      <el-input v-model="doc.publication.title" placeholder="请输入出版物标题"/>
    </el-form-item>
    <el-form-item label="出版物ISBN">
      <el-input v-model="doc.publication.isbn" placeholder="请输入出版物ISBN" @input="checkIsbn"/>
    </el-form-item>
    <el-form-item label="出版物ISSN">
      <el-input v-model="doc.publication.issn" placeholder="请输入出版物ISSN" @input="checkIssn"/>
    </el-form-item>
    <el-form-item label="出版物类别">
      <el-input v-model="doc.publication.category" placeholder="请输入出版物分类"/>
    </el-form-item>
    <el-form-item label="出版物期号">
      <el-input v-model="doc.publication.issue" placeholder="请输入出版物期号"/>
    </el-form-item>
    <el-form-item label="出版物卷号">
      <el-input v-model="doc.publication.volume" placeholder="请输入出版物卷号"/>
    </el-form-item>
    <el-form-item label="出版时间">
      <el-date-picker v-model="doc.publication.date" type="date" placeholder="请选择出版日期" style="width: 100%"/>
    </el-form-item>
    <author-list-component v-model="doc.authors" :has="'true'"/>
    <quotation-list-component v-model="doc.quotations"/>
    <fund-list-component v-model="doc.funds"/>

    <br><br>
    <el-form-item style="float: right">
      <el-button type="primary" size="large" @click="onSubmit">确认编辑</el-button>
    </el-form-item>
    <br><br>
  </el-form>
</template>

<script setup>
import {onMounted, ref, provide} from "vue";
import {editDocumentApi, searchByDoi} from "@/apis/document";
import {ElMessage} from "element-plus";
import AuthorListComponent from "@/components/add/document/author/AuthorListComponent.vue";
import QuotationListComponent from "@/components/add/document/quotation/QuotationListComponent.vue";
import FundListComponent from "@/components/add/document/fund/FundListComponent.vue";
import AddOneAuthorComponent from "@/components/add/document/author/AddOneAuthorComponent.vue";

const doc = ref({})
const doi = ref('')
const kws = ref('')

// 根据DOI搜索待编辑的未读
const search = () => {
  if (!doi.value) doi.value = ''
  if (doi.value.trim()) {
    searchByDoi(doi.value.trim()).then(resp => {
      console.log(resp.data)
      if (resp.data.code === 200) {
        doc.value = resp.data.data;
        if (doc.value?.content?.keywordsStr) {
          kws.value = doc.value.content.keywordsStr
        }
      } else {
        ElMessage.info(resp.data.msg)
      }
    })
  } else {
    ElMessage.error('请输入查询的文档DOI')
  }
}

provide('doc', doc)

// 第一作者，通讯作者展示形式
const authorDesc = (author) => author.firstName + '【' + author.lastName + '】,【' + author.email + '】';

// 展示一个作者的详细信息
const authorDetail = ref({})
const authorShowDialog = ref(false)
provide('showAuthor', authorDetail)
provide('authorShowDialog', authorShowDialog)
const showAuthorDesc = () => {
  authorShowDialog.value = true
}
const showAuthor = (author) => {
  authorDetail.value = author
  showAuthorDesc()
}

// 删除一个作者
const delAuthor = (flag) => {
  if (confirm('确定要删除' + (flag ? '第一作者' : '通讯作者') + '吗?')) {
    if (flag) doc.value.firstAuthor = undefined
    else doc.value.correspondingAuthor = undefined
  }
}

// 限制关键词输入格式
const checkKeywordsInput = () => {
  if (kws.value) {
    kws.value = kws.value.replace('；', ';')
    kws.value = kws.value.replace(/[^\u4e00-\u9fa5；;a-zA-Z0-9]/g, ''); // 移除非中文字符和中英文分号
    kws.value = kws.value.replace(';;', ';')
    if (kws.value[0] === ';') {
      kws.value = kws.value.substring(1);
    }
  }
}

const checkDoi = () => {
  const str = doc.value.doi
  doc.value.doi = str.replace(' ', '')
}

const checkIsbn = () => {
  const str = doc.value.publication.isbn
  doc.value.publication.isbn = str.replace(' ', '')
}

const checkIssn = () => {
  const str = doc.value.publication.issn
  doc.value.publication.issn = str.replace(' ', '')
}

onMounted(() => {
  doi.value = sessionStorage.getItem('edit_doc_doi')
  if (doi.value) {
    search()
    sessionStorage.removeItem('edit_doc_doi')
  }
})

// 修改文档函数
const onSubmit = () => {
  // 先处理关键词字符串
  if (!kws.value) kws.value = ''
  let ckws = kws.value.trim().split(';')
  const len = ckws.length
  if (len <= 0 || len === 1 && !ckws[len - 1]) {
    ElMessage.error('未填写关键词')
    return
  }
  if (!ckws[len - 1]) ckws = ckws.slice(0, -1)
  for (let i = 0; i < ckws.length; i++) ckws[i] = ckws[i].trim()
  doc.value.content.keywords = []
  new Set(ckws).forEach(kw => doc.value.content.keywords.push({name: kw}))

  // 处理关键词字符串完成
  console.log(doc.value)
  editDocumentApi(doc.value).then(resp => {
    if (resp.data.code === 200) {
      ElMessage.success("编辑成功")
      doc.value = {}
    } else {
      ElMessage.error(resp.data.msg)
    }
  })
}


</script>

<style scoped>
.author-desc {
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unselect-author {
  color: red;
  font-style: italic;
  font-weight: bold;
  float: left;
}
</style>